//
//  PublishTextViewCell.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/3/7.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

protocol PublishTextViewCellDelegate: NSObjectProtocol {
    func textViewCell(_ cell: PublishTextViewCell, didEndEdit vm: PublishViewModel)
}

class PublishTextViewCell: UITableViewCell, PublishTextViewDelegate {
    
    static let reuseIdentifier_0 = "publishTextViewCellId_0"
    static let reuseIdentifier_1 = "publishTextViewCellId_1"
    static let reuseIdentifier_2 = "publishTextViewCellId_2"
    
    weak var delegate: PublishTextViewCellDelegate?
    
    private var indexPath = IndexPath(row: 0, section: 2)
    
    private var viewModel = PublishViewModel()
    
    @IBOutlet weak var textView: PublishTextView! {
        didSet {
            textView.text = ""
            textView.tvDelegate = self
        }
    }
    
    func configure(for indexPath: IndexPath, vm: PublishViewModel, delegate: PublishTextViewCellDelegate?) {
        self.viewModel = vm
        self.delegate = delegate
        self.indexPath = indexPath
        textView.isUserInteractionEnabled = !vm.patientId.isEmpty
        if indexPath.section == 2 {
            textView.text = vm.desc
            textView.placeholder = "请描述您的症状、以往就诊或用药信息，便于医生进行准确分析"
            textView.maxNumberOfWords = 300
        } else if indexPath.section == 3 {
            textView.placeholder = "如有过敏史，请如实填写"
            textView.maxNumberOfWords = 50
        } else {
            textView.placeholder = ""
            textView.maxNumberOfWords = 100
        }
    }
    
    func updateData(for indexPath: IndexPath, vm: MyCloudReadMeDetailViewModel) {
        self.indexPath = indexPath
        textView.placeholder = ""
        self.isUserInteractionEnabled = false
        if indexPath.section == 2 {
            textView.text = vm.oneselfState
        } else if indexPath.section == 3 {
            textView.text = vm.allergyHistory
        } else {
            textView.text = vm.pastHistory
        }
    }
    
    func configure(with vm: AdvisoryDetailViewModel, indexPath: IndexPath) {
        textView.isUserInteractionEnabled = false
        if indexPath.section == 2 {
            textView.text = vm.oneselfState
            textView.maxNumberOfWords = 300
        } else if indexPath.section == 3 {
            textView.text = vm.allergyHistory_t
            textView.maxNumberOfWords = 50
        } else {
            textView.text = vm.pastHistory_t
            textView.maxNumberOfWords = 100
        }
    }
    
    func textViewDidEndEditing(_ textView: UITextView) {
        switch indexPath.section {
        case 2:
            viewModel.desc = textView.text!
        case 3:
            viewModel.allergyHistory = textView.text!
        default:
            viewModel.pastHistory = textView.text!
        }
        
        delegate?.textViewCell(self, didEndEdit: viewModel)
    }
}
